package rexsee.natives;

import rexsee.core.utilities.Utilities;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;

public class CustomizedGallery extends Gallery {

	private OnTransformListener mListener = null;

	public CustomizedGallery(Context context) {
		super(context);
		this.setStaticTransformationsEnabled(true);
		setDefaultTransformListener();
	}

	@Override
	protected boolean getChildStaticTransformation(View child, Transformation t) {
		t.clear();
		t.setTransformationType(Transformation.TYPE_MATRIX);
		if (mListener != null) {
			int center = (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();
			int childCenter = child.getLeft() + child.getWidth() / 2;
			float position = (float) (center - childCenter) / child.getWidth();
			return mListener.run(child, t, position);
		} else return false;
	}

	public void setTransformListener(OnTransformListener listener) {
		mListener = listener;
	}
	public void setDefaultTransformListener() {
		OnTransformListener transformListener = new OnTransformListener() {
			private final Camera mCamera = new Camera();
			@Override
			public boolean run(View child, Transformation t, float position) {
				ViewTag tag = (ViewTag) CustomizedGallery.this.getTag();
				if (tag == null) return false;
				position = (position > 1) ? 1 : position;
				position = (position < -1) ? -1 : position;
				mCamera.save();
				final Matrix imageMatrix = t.getMatrix();
				boolean translateZReverse = !tag.style.zoom_reverse.equalsIgnoreCase("false");
				float translateZ = (translateZReverse) ? (1f - Math.abs(position)) * Utilities.getInt(tag.style.zoom, 0) : (1f - position) * Utilities.getInt(tag.style.zoom, 0);
				if (translateZ != 0) mCamera.translate(0.0f, 0.0f, translateZ);
				String[] rotation = tag.style.rotation.split("\\ ");
				int rotationX, rotationY, rotationZ;
				String[] rotationReverse = tag.style.rotation_reverse.split("\\ ");
				boolean rotationXReverse, rotationYReverse, rotationZReverse;
				if (rotationReverse.length == 3) {
					rotationXReverse = rotationReverse[0].equalsIgnoreCase("true");
					rotationYReverse = rotationReverse[1].equalsIgnoreCase("true");
					rotationZReverse = rotationReverse[2].equalsIgnoreCase("true");
				} else {
					rotationXReverse = rotationYReverse = rotationZReverse = rotationReverse[0].equalsIgnoreCase("true");
				}
				if (rotation.length == 3) {
					rotationX = (rotationXReverse) ? (int) (Math.abs(position) * Utilities.getInt(rotation[0], 0)) : (int) (position * Utilities.getInt(rotation[0], 0));
					rotationY = (rotationYReverse) ? (int) (Math.abs(position) * Utilities.getInt(rotation[1], 60)) : (int) (position * Utilities.getInt(rotation[1], 60));
					rotationZ = (rotationZReverse) ? (int) (Math.abs(position) * Utilities.getInt(rotation[2], 0)) : (int) (position * Utilities.getInt(rotation[2], 0));
				} else {
					rotationX = 0;
					rotationY = (rotationYReverse) ? (int) (Math.abs(position) * Utilities.getInt(rotation[0], 60)) : (int) (position * Utilities.getInt(rotation[0], 60));
					rotationZ = 0;
				}
				if (rotationX != 0) mCamera.rotateX(rotationX);
				if (rotationY != 0) mCamera.rotateY(rotationY);
				if (rotationZ != 0) mCamera.rotateZ(rotationZ);
				mCamera.getMatrix(imageMatrix);
				final int imageHeight = child.getLayoutParams().height;
				final int imageWidth = child.getLayoutParams().width;
				imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
				imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
				mCamera.restore();
				return true;
			}
		};
		setTransformListener(transformListener);
	}

	public static abstract class OnTransformListener {
		public abstract boolean run(View child, Transformation t, float position);
	}

}
